﻿// -----------------------------------------------------------------------
// <copyright file="PlaylistMask.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.Player.Playlist
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Metal.Player.Playlist;

    /// <summary>
    /// Playlist mask
    /// </summary>
    public partial class PlaylistMask : Gui.BaseMask.BaseMask, IPlaylistView
    {
        #region Members

        /// <summary> Represents the controller for this mask </summary>
        private PlaylistController controller;

        #endregion

        #region Constructor

        /// <summary> Initializes a new instance of the PlaylistMask class. </summary>
        public PlaylistMask()
        {
            try
            {
                this.InitializeComponent();

                this.controller = new PlaylistController(this);

                this.ApplyTheme();

                // Subscribe to menu items click event
                if (GetControl("MenuItems") != null)
                {
                    (GetControl("MenuItems") as Controls.MetalTopMenu).OnMenuClickEvent += new EventHandler(this.OnMenuClick);
                }

                if (GetControl("List") != null)
                {
                    GetControl("List").DragDrop += new DragEventHandler(this.OnPlaylistDragDrop);
                }

                // Subscribe to on media load event (used to switch between audio / video style)
                Core.Player.Events.Instance.BeforeOpen += new EventHandler(OnBeforeOpenFile);

                Gui.Presentation.FormsManager.SwitchAudioVideoStyle += new EventHandler(SwitchStyle);
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        private void SwitchStyle(object sender, EventArgs e)
        {
            if (this.IForm.Visible == true)
            {
                this.controller.SetCurrentView(this);
            }
        }

        #endregion

        #region Events declaration

        /// <summary> Event when the user drops a file or folder in playlist </summary>
        public event DragEventHandler ListDragDrop;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the playlist data set
        /// </summary>
        public DataSet ListDataSet
        {
            get
            {
                return (GetControl("List") as Gui.Controls.MetalList).ListDataSet;
            }

            set
            {
                (GetControl("List") as Gui.Controls.MetalList).ListDataSet = value;
            }
        }

        /// <summary>
        /// Gets or sets the playlists data set
        /// </summary>
        public DataSet PlaylistsDataSet
        {
            get
            {
                return (GetControl("Playlists") as Gui.Controls.MetalList).ListDataSet;
            }

            set
            {
                (GetControl("Playlists") as Gui.Controls.MetalList).ListDataSet = value;
            }
        }

        /// <summary>
        /// Gets the selected item
        /// </summary>
        public DataGridViewRow ListSelectedRow
        {
            get
            {
                if ((GetControl("List") as Gui.Controls.MetalList).SelectedRows.Count > 0)
                {
                    return (GetControl("List") as Gui.Controls.MetalList).SelectedRows[0];
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Gets the selected item
        /// </summary>
        public DataGridViewRow PlaylistsSelectedRow
        {
            get
            {
                if ((GetControl("Playlists") as Gui.Controls.MetalList).SelectedRows.Count > 0)
                {
                    return (GetControl("Playlists") as Gui.Controls.MetalList).SelectedRows[0];
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the playlist is visible or not
        /// </summary>
        public bool PlaylistsVisible
        {
            get
            {
                return (GetControl("Playlists") as Gui.Controls.MetalList).Visible;
            }

            set
            {
                (GetControl("Playlists") as Gui.Controls.MetalList).Visible = value;
            }
        }

        /// <summary>
        /// Sets the selected row
        /// </summary>
        public int SelectPlaylistsRow
        {
            set
            {
                if ((GetControl("Playlists") as Gui.Controls.MetalList).SelectedRows.Count > 0)
                {
                    // Un-select the selected row
                    (GetControl("Playlists") as Gui.Controls.MetalList).SelectedRows[0].Selected = false;
                }

                (GetControl("Playlists") as Gui.Controls.MetalList).Rows[value].Selected = true;
            }
        }

        /// <summary>
        /// Sets the selected row
        /// </summary>
        public int SelectListRow
        {
            set
            {
                if ((GetControl("List") as Gui.Controls.MetalList).SelectedRows.Count > 0)
                {
                    // Un-select the selected row
                    (GetControl("List") as Gui.Controls.MetalList).SelectedRows[0].Selected = false;
                }

                (GetControl("List") as Gui.Controls.MetalList).Rows[value].Selected = true;
            }
        }

        public int ScrollListToIndex
        {
            set
            {
                (GetControl("List") as Gui.Controls.MetalList).FirstDisplayedScrollingRowIndex = value;
            }
        }

        #endregion

        #region Event methods

        /// <summary>
        /// This event occurs when the user clicks on a menu, and calls by reflection the desired method
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnMenuClick(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem control = (ToolStripMenuItem)sender;
                string methodName = control.Name;

                Type thisType = typeof(PlaylistLogic);
                System.Reflection.MethodInfo theMethod = thisType.GetMethod(methodName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                if (theMethod != null)
                {
                    theMethod.Invoke(PlaylistLogic.Instance, null);
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Occurs when the user drops files or folders in playlist
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnPlaylistDragDrop(object sender, DragEventArgs e)
        {
            this.ListDragDrop(sender, e);
        }

        #endregion

        /// <summary>
        /// Switch to video style if opened file is a video format
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OnBeforeOpenFile(object sender, EventArgs e)
        {
            try
            {
                if (this.Visible == false)
                {
                    return;
                }

                this.controller.SetCurrentView(this);
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }
    }
}
